#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <istream>
#include <set>
#include <unordered_map>
#include <vector>
using ll = int64_t;

ll n,k,ans;
std::vector<ll> ap,bp,cnt;
std::unordered_map<ll, ll> ans1,ans2;
std::set<ll> vis;
#define printf
int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n>>k;
    ap.resize(n+1),bp.resize(n+1),cnt.resize(n+1);
    for(ll i=1;i<=k;i++){
        ll tmp;
        std::cin>>tmp;
        ap[tmp]=i;
        cnt[tmp]++;
        vis.emplace(tmp);
    }
    for(ll i=1;i<=k;i++){
        ll tmp;
        std::cin>>tmp;
        cnt[tmp]++;
        bp[tmp]=i;
        if(cnt[tmp]==2){
            printf("cnt[%lld]=%lld\n",tmp,cnt[tmp]);
            ll diff=ap[tmp]-bp[tmp];
            if(diff<0)diff+=k;
            printf("diffa=%lld\n",diff);
            ans1[diff]++;
            ans=std::max({ans1[diff],ans});

            diff=ap[tmp]-(k-bp[tmp]+1);
            if (diff<0) {
                diff+=k;
            }
            printf("diffb=%lld\n",diff);
            ans2[diff]++;
            ans=std::max({ans2[diff],ans});
        }
        vis.emplace(tmp);
    }
    printf("ans=%lld, vis.size()=%lld\n",ans,vis.size());
    std::cout<<n-vis.size()+ans<<"\n";
}